require 'dict'
require 'lexsem_crbm'


function main() 
	-- load data & dic
	f = torch.DiskFile(data_path, 'r')
	f:binary()
	f:readObject()
	tw_dic = f:readObject()
	setmetatable(tw_dic, Dict_mt)
	f:close()

	f = torch.DiskFile(dicemb_path, 'r')
	dic = f:readObject()
	setmetatable(dic, Dict_mt)
	f:close()

	-- load model
	print('load model')
	model = LexSemCRBM:load_from_file(model_path, 'r')
	emb = model.t_emb
	count = model.t_count
	ntypes = model.ntypes
	
	-- extract embs
	f = torch.DiskFile(output_path, 'w')
	f:noAutoSpacing()

	targets = {}
	targets['bank'] = 1
	targets['star'] = 1
	targets['oracle'] = 1
	targets['sun'] = 1
	targets['jaguar'] = 1

	for i = 1,tw_dic:size() do
		local word = dic.id2word[tw_dic.id2word[i]] print(word)
		local freq = count[{i,{}}]
		freq = freq:div(freq:sum()):mul(100):long()
		min_t = 1
		max_t = 5
		min = 0
		if targets[word] == nil then
			_,id = freq:max(1)
			min_t = id[{1}]
			max_t = id[{1}]
			min = 20
		end
		for t = min_t,max_t do
			if freq[{t}] > min then
			f:writeString(word .. freq[{t}] .. ' ')
			--f:writeString(word .. t .. ' ')
			e = emb[{i,t,{}}] 
			for j = 1,e:numel() do
				f:writeDouble(e[j])
				f:writeString(' ')
			end
			f:writeString('\n')
			end
		end
	end

	f:close()
end

if #arg == 4 then
	data_path	= arg[1]
	dicemb_path	= arg[2]
	model_path	= arg[3]
	output_path	= arg[4]
	main()
else
	print('<data path> <dic emb path> <model path> <output path>')
end

